import { axios } from '@/utils/request'
import baseapi from '@/api/base'
import { getDataByParameter, saveObjetInfo, deleteObjetInfo } from '@/utils/baserequest'
import storage from 'store'

const api = {
  menuServer: '/menu/',
  menus: '/menu/by/parentMenuPid/',
  menusCount: '/countBy/parentMenuPid/',
  huiwAppServer: '/appId/',
  huiwApps: '/appId/like/appInfo/',
  // orgPid
  orgPid: storage.get('org')
}

export default api // getMenuByParentMenu, getMenuCountByParentMenu, getMenuInfo, delMenu

// 得到appId 列表
export function getHuiwApps (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    authOption: '1'
  }
  const parameters = {
    appInfo: ' ',
    pageSize: 100000
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.huiwApps
  const allheaders = Object.assign(headers, appIdHeaders)
  console.log(url, parameters, allheaders)
  return getDataByParameter(url, parameters, allheaders)
}

// 得到下级菜单
export function getMenuByParentMenu (parentMenuPid, appId, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: '',
    authOption: '1'
  }
  const parameters = {
    parentMenuPid: parentMenuPid,
    appId: appId,
    pageSize: 100000
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.menus
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
  // .then(res => {
  //   return res
  // })
}

// 得到下级菜单
export function getMenuCountByParentMenu (parentMenuPid, appId, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  return axios({
    url: baseapi.baseUrl + baseapi.huiwcore + api.menusCount + appId + '/' + parentMenuPid,
    method: 'get',
    headers: Object.assign(headers, allheaders),
    parameter: {}
  })
}

// 得到菜单详细信息
export function getMenuInfo (parameter, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  return axios({
    url: baseapi.baseUrl + baseapi.huiwcore + api.menuServer + parameter,
    headers: Object.assign(headers, allheaders),
    method: 'get'
    // params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveMenuInfo (parameter, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const data = {
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.menuServer
  return saveObjetInfo(url, Object.assign(data, parameter), Object.assign(headers, allheaders))
}

// 删除菜单
export function delMenu (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.menuServer
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, parameter, allheaders)
}
